<?php

namespace BWL\webshopBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use JMS\SecurityExtraBundle\Annotation\Secure;

class PageController extends Controller
{
  public function homeAction() {
    return $this->render('BWLwebshopBundle:Page:home.html.twig');
  }

  public function aboutAction() {
    return $this->render('BWLwebshopBundle:Page:about.html.twig');
  }

  public function productsAction() {
    $em = $this->getDoctrine();
    $items = $em->getRepository('BWLwebshopBundle:Item')->findAll();

    if(!$items) {
      throw $this->createNotFoundException('No product found.');
    }
    return $this->render('BWLwebshopBundle:Page:showProducts.html.twig', array('items' => $items));
  }


  public function searchAction() {

    return $this->render('BWLwebshopBundle:Page:search.html.twig');
  }




  /**
   * @Secure(roles="ROLE_ADMIN")
   */
  public function adminAction() {
    return $this->render('BWLwebshopBundle:Admin:home.html.twig');
  }


  public function searchResultAction() {
    $em = $this->getDoctrine();
    $result = $em->getRepository("BWL\webshopBundle\Entity\Item")->createQueryBuilder('i')
      ->where('i.name LIKE :query')
      ->orWhere('i.description LIKE :query')
      ->setParameter('query', '%' . $_GET['query'] . '%')
      ->getQuery()
      ->getResult();

    //No search Results found
    if(!$result) {
      return $this->render('BWLwebshopBundle:Page:noProductFound.html.twig', array('query' => $_GET['query']));
    }
    return $this->render('BWLwebshopBundle:Page:showProducts.html.twig', array('items' => $result, 'name' => 'Suchergebnisse'));
  }

}
